package com.mz.ozy.utils;

import android.content.Context;
import android.content.SharedPreferences;
import android.text.TextUtils;

/**
 * Created by Administrator on 2017/8/3.
 * 
 */

public class SharePreferencesData {
     public static SharedPreferences initSharePreferences(Context context){
         SharedPreferences sp = context.getSharedPreferences("user", Context.MODE_PRIVATE);
         return sp;

     }

    /**
     * 添加数据
     * @param key
     * @param value
     */
     public static void addSharePreferencesData(Context context, String key, String value){
         SharedPreferences.Editor edit = initSharePreferences(context).edit();
         edit.putString(key,value);
         edit.commit();
     }

    public static void putInt(Context context, String key, int value){
        SharedPreferences.Editor edit = initSharePreferences(context).edit();
        edit.putInt(key,value);
        edit.commit();
    }

    public static int getInt(Context context, String key){
        return initSharePreferences(context).getInt(key,-1);
    }


    public static void setBoolean(Context context, String key, Boolean b){
        SharedPreferences.Editor edit = initSharePreferences(context).edit();
        edit.putBoolean(key,b);
        edit.commit();
    }

    public static boolean getBoolean(Context context, String key){
        return initSharePreferences(context).getBoolean(key,false);
    }


    /**
     * 获取数据
     * @param key
     */
    public static String getSharePreferencesData(Context context, String key){
        return initSharePreferences(context).getString(key,"");

    }

    /**
     * 删除数据
     * @param key
     */
    public static void deleteSharePreferencesData(Context context, String key){
        SharedPreferences.Editor edit = initSharePreferences(context).edit();
        edit.putString(key,"");
        edit.commit();
    }

    public static void deleteInt(Context context, String key){
        SharedPreferences.Editor edit = initSharePreferences(context).edit();
        edit.putInt(key,-1);
        edit.commit();
    }

    /**
     * 是否存在数据
     * @param context
     * @param key
     * @return
     */
    public static boolean isExistData(Context context, String key){
        if(!TextUtils.isEmpty(getSharePreferencesData(context,key))){
            return true;
        }else{
            return false;
        }
    }
    
}
